//
//  GatheringDetailViewController.m
//  HAODE
//
//  Created by panquanReabam.com on 15/12/10.
//  Copyright © 2015年 reabam.com. All rights reserved.
//

#import "ScanReceiptsViewController.h"
#import "RDVTabBarController.h"
#import "UIViewExt.h"
#import "HttpRequest.h"
#import "RDVTabBarController.h"
#import "UIButton+WebCache.h"
#import "PayForSuccessViewController.h"
@interface ScanReceiptsViewController ()<MBProgressHUDDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITextFieldDelegate,UISearchBarDelegate,UIScrollViewDelegate>
@end

@implementation ScanReceiptsViewController
{
    UIButton*tureBtn;
    UIButton*time;
}

-(void)BackAction:(UIButton *)sender{
    [time removeFromSuperview];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidDisappear:(BOOL)animated
{[super viewDidDisappear:animated];
    self.navigationController.delegate = nil;
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavi];
    self.view.backgroundColor=[UIColor whiteColor];
    UILabel *payAmountLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,ScreenHeight*170/1335+64, ScreenWidth, 30)];
    payAmountLabel.font=[UIFont systemFontOfSize:35];
    payAmountLabel.textAlignment=NSTextAlignmentCenter;
    NSString*GoldPrice=[NSString stringWithFormat:@"%@",self.payAmount];
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setPositiveFormat:@"###,##0.00;"];
    NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:GoldPrice.floatValue]];
    payAmountLabel.text=[NSString stringWithFormat:@"¥ %@",formattedNumberString];
    
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"¥ %@" ,formattedNumberString]];
    
    [AttributedStr addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:30.0]
     
                          range:NSMakeRange(0, 1)];
    
    [AttributedStr addAttribute:NSForegroundColorAttributeName
     
                          value:backColor
                          range:NSMakeRange(0, 1)];
    payAmountLabel.attributedText = AttributedStr;
    
    [self.view addSubview:payAmountLabel];
    [tureBtn removeFromSuperview];
    
    tureBtn = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/2-85,ScreenHeight*400/1335+50, 170, 170)];
    [tureBtn setImageWithURL:[NSURL URLWithString:[self.dic  objectForKey:@"qrCode"]]];
    tureBtn.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:tureBtn];
    [self  times];

    UILabel* PayLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, tureBtn.bottom+20, ScreenWidth,13.0)];
    PayLabel .text=@"对准二维码，即可自动扫描支付" ;
    PayLabel.textColor=zhonghuiColor;
    PayLabel.textAlignment=NSTextAlignmentCenter;
    PayLabel.font = [UIFont systemFontOfSize: 13.0];
    [self.view addSubview:PayLabel];
    
    UILabel* ToObtain = [[UILabel alloc] initWithFrame:CGRectMake(20, 25+64, 120,26)];
    ToObtain .text=@"账单总计" ;
    ToObtain.textColor=[UIColor blackColor];
    ToObtain.font = [UIFont systemFontOfSize: 15.0];
    [self.view addSubview:ToObtain];
    
 
    
}

- (void)times{
    
    time = [[UIButton alloc]init];
    time.frame = CGRectMake(ScreenWidth-100,ScreenHeight*35/2200,30,16);
    [time setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
    [time setTitle:[NSString stringWithFormat:@"%d",10] forState:UIControlStateNormal];
    time.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [self.view addSubview:time];
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownActions:) userInfo:time repeats:YES];
}
//自动更新二维码是否收银成功
-(void)countDownActions:(NSTimer*)timer{
    time = timer.userInfo;
    if (!time.superview) {
        [timer invalidate];
        return;
    }
    int currentTime = [[time titleForState:UIControlStateNormal] intValue];
    [time setTitle:[NSString stringWithFormat:@"%d",--currentTime] forState:UIControlStateNormal];
    time.titleLabel.font = [UIFont systemFontOfSize:16];
    if (currentTime<1) {
        
        [time setTitle:[NSString stringWithFormat:@"%d",10] forState:UIControlStateNormal];
        [self times];
        [time removeFromSuperview];
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSMutableDictionary *params = [NSMutableDictionary new];
        [params setValue:[user objectForKey:@"tokenId"] forKey:@"tokenId"];
        [params setValue:[self.dic objectForKey:@"gid"] forKey:@"gid"];
        [HttpRequest requestData:@"Business/Gathering/Detail" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
               NSLog(@"－－%@",severDataDict.dataDict);
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    [Global hideProgressHUDInternal:self.view];
                    if ([[severDataDict.dataDict  objectForKey:@"status"] isEqualToString:@"N"]) {
                    }if ( [[severDataDict.dataDict  objectForKey:@"status"] isEqualToString:@"Y"]) {
                        
                        [time removeFromSuperview];
                        [timer invalidate];
                        PayForSuccessViewController *detailViewController = [[PayForSuccessViewController alloc] init];
                        detailViewController.orderId=[severDataDict.dataDict objectForKey:@"orderId"];
                        detailViewController.payAmount=[NSString stringWithFormat:@"%.2f",  [[severDataDict.dataDict objectForKey:@"payAmount"]doubleValue]];
                        detailViewController.memberId=self.memberId;
                        detailViewController.orderType=[severDataDict.dataDict objectForKey:@"orderType"];
                        [self.navigationController pushViewController:detailViewController animated:YES];
                        
                    }
                }else{
                    
                }
            }
            else
            {
                [Global hideProgressHUDInternal:self.view];
                [timer invalidate];
                [Global showTextHudSub:severDataDict.resultStr view:self.view];}
        }];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)setNavi{
    [[self rdv_tabBarController] setTabBarHidden:YES];
    
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
    
    self.title=@"扫码收款";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor blackColor],
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    
    
    
    
}

@end
